﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using SJ.Platform.Common;
using SJ.Platform.EFCore.Model.LogManager;
using SJ.Platform.Cache.RabbitMQ;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace SJ.Platform.Extend.IOC.Operation
{
    /// <summary>
    /// 操作记录实现
    /// </summary>
    public class OperationLogImp : IOperationLogger, IDependency
    {
        private readonly OperationConfig _operationConfig;
        private readonly RabbitMQConfig _rabbitMqConfig;
        public OperationLogImp(IOptions<CustomConfig> customConfig) 
        {
            _operationConfig = customConfig.Value.OperationConfig;
            _rabbitMqConfig = customConfig.Value.RabbitMQConfig;

            //初始化RabbitMq
            RabbitMQConnection.InitRabbitMQConnection(_rabbitMqConfig, RabbitMqReceiveFunc, RabbitMqNextAction);
        }
        public async Task AddOperationLogger(string token, string actionName, string userId, string ip, string details)
        {
            try
            {
                var actionLogModel = new SysLog
                {
                    Id = Guid.NewGuid().ToString(),
                    ActionName = actionName,
                    UserId = userId,
                    Ip = ip,
                    Details = details
                };
                var rabbitMqMsg = new RabbitMQMessage<SysLog> { token = token, t = actionLogModel };

                //使用RabbitMQ处理操作日志的写入
                RabbitMQConnection.PublishExchange(JsonSerializer.Serialize(rabbitMqMsg), _rabbitMqConfig.ExchangeName, _rabbitMqConfig.RoutingKey);
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 消息处理
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public async Task<bool> RabbitMqReceiveFunc(string msg)
        {
            bool result = true;
            try
            {
                var rabbitMqMsg = JsonSerializer.Deserialize<RabbitMQMessage<SysLog>>(msg);
                var response = await InterfaceHandle.PostAsync(_operationConfig.BaseUrl, string.Format(_operationConfig.OperationAdd, GlobalVariableHandle.Scope_LogApi), rabbitMqMsg.token, JsonSerializer.Serialize(rabbitMqMsg.t));
                // 使用 Newtonsoft.Json 反序列化
                if (response != null)
                {
                    JObject jsonObject = JObject.Parse(response.ToString());
                    // 取出 data 字段的值
                    result = (bool)jsonObject["data"];
                }
            }
            catch
            {
                result = false;
            }
            return result;
        }
        /// <summary>
        /// 消息处理完成后的回调
        /// </summary>
        public void RabbitMqNextAction(bool result)
        {
            return;
        }
    }
}
